exports.updatePayOrder = function (req, res) {

    //获取bodyParams参数
    var bodyParams = req.routeInfo.bodyParams;

    var productName = '';

    var resultJson = bodyParams.xml;
    console.log("/-/-/-/-/-/-/-/-/");
    console.log(resultJson);
    var attachJson = JSON.parse(resultJson.attach);
//  var attachJson = bodyParams.attach;
    console.log("bodyParams:");
    console.log(bodyParams);
    console.log("attachJson:");
    console.log(attachJson);
    //任务流创建
    async.waterfall([

        //更新订单状态
        function (cb) {
            var params = {
                apo_no: attachJson.o_no,
                apo_status: 1
            };

            R.SEND_HTTP(
                req, {
                    url: CORE_APIConfig.coreAPI.organizationUpdateArtProductOrder,
                    method: 'POST',
                    data: params
                },
                function (err, data) {

                    cb();

                }
            );

        },
        //生成购买记录
        function (cb) {

            //1年的秒数
            var yearTime = 31536000;
            console.log('查询是否有该记录')
            console.log(CORE_APIConfig.coreAPI.organizationSearchArtPeoductRecord + "/apur_oid/" + attachJson.apo_oid + "/apur_apid/" + attachJson.apo_pid)

            //查询是否有该记录
            R.SEND_HTTP(
                req, {
                    // url: CORE_APIConfig.coreAPI.organizationSearchArtPeoductRecord + "/apur_oid/682/apur_apid/1",
                    url: CORE_APIConfig.coreAPI.organizationSearchArtPeoductRecord + "/apur_oid/" + attachJson.apo_oid + "/apur_apid/" + attachJson.apo_pid,
                    method: 'GET'
                },
                function (err, data) {
                    var params = 0;
                    //更新操作
                    if (data.data.length > 0) {
                        var apur_id = data.data[0].apur_id;
                        var old_apur_end_time = parseInt(data.data[0].apur_end_time);
                        params = {
                            apur_id: apur_id,
                            apur_end_time: old_apur_end_time + yearTime
                        };
                        R.SEND_HTTP(
                            req, {
                                url: CORE_APIConfig.coreAPI.organizationUpdateArtPeoductRecord,
                                method: 'POST',
                                data: params
                            },
                            function (err, data) {
                                cb();
                            }
                        );
                        //新增操作
                    } else {
                        params = {
                            apur_oid: attachJson.apo_oid,
                            apur_apid: attachJson.apo_pid,
                            apur_end_time: common.nowTime(false) + yearTime
                        };
                        R.SEND_HTTP(
                            req, {
                                url: CORE_APIConfig.coreAPI.organizationAddartPeoductRecord,
                                method: 'POST',
                                data: params
                            },
                            function (err, data) {
                                cb();
                            }
                        );
                        cb();
                    }
                }
            );
        },

        //生成机构信息
        function (cb) {
            // return cb();

            R.SEND_HTTP(
                req,
                {
                    url: "http://127.0.0.1:3400/organization/search_art_product_order/apo_no/" + attachJson.o_no,
                    method: 'POST',
                    data: {
                        fields: ["apo_campus_name", "apo_school_count", "apo_type", "apo_apid", "apo_name", "apo_buy_time", "apo_oid", "apo_id", "apo_phone"].join(",")
                    }
                },
                function (err, data) {
                    var orderInfo = data.data;

                    console.log("有没有订单信息");
                    console.log(orderInfo);

                    if (orderInfo.length > 0) {
                        R.SEND_HTTP(
                            req, {
                                url: "http://127.0.0.1:3500/erpUser/search/ou_username/" + orderInfo[0].apo_campus_name,
                                method: 'POST',
                            },
                            function (err, data) {

                                var userExist = data.data;

                                console.log("有没有用户");
                                productName = orderInfo[0].apo_name;
                                console.log(userExist);
                                if (userExist.length > 0) {
                                    cb();
                                } else {
                                    var params = {
                                        username: orderInfo[0].apo_campus_name,//账号
                                        schoolCount: orderInfo[0].apo_school_count,//购买校区数量
                                        createType: orderInfo[0].apo_type,//1-新购买 2-正常购买 3-续费
                                        apo_apid: orderInfo[0].apo_apid,//产品id
                                        year: parseInt(orderInfo[0].apo_buy_time) > 3 ? 0.25 : orderInfo[0].apo_buy_time,//多少年
                                        oid: orderInfo[0].apo_oid,//createType为1时传0，不为1时传机构id
                                        apo_no: attachJson.o_no,
                                        apo_phone: orderInfo[0].apo_phone,
                                    };
                                    console.log("*-*-*-*-*-*-*-*-*");
                                    console.log(params);
                                    R.SEND_HTTP(
                                        req, {
                                            url: "http://127.0.0.1:3500/common/createOrganizationAndEnterpriseStructure",
                                            method: 'POST',
                                            data: params
                                        },
                                        function (err, data) {
                                            console.log(err);
                                            console.log(data);
                                            console.log('走接口了');
                                            cb();
                                        }
                                    );
                                }
                            }
                        );

                    } else {
                        console.log("订单不存在");
                        cb()
                    }
                }
            );

        },

        function (cb) {
            console.log('---------准备发送销售管理员私信');
            if (attachJson.sell_id) {

                var wxParams = {
                    u_id: attachJson.sell_id,
                    productName: productName,
                };
                var wxUrls = "http://www.hxzbi.com:8800/weixin/sendManagerMessage";

                R.SEND_HTTP(
                    req, {
                        url: wxUrls,
                        method: 'POST',
                        data: wxParams
                    },
                    function (err, data) {
                        console.log('销售管理员私信已发送');
                        cb();
                    }
                );
            } else {
                console.log('没有销售管理员');
                cb();
            }

        }
    ], function (err, result) {
        return res.send('success');
    })
}